<template>
  <div>
    <el-card class="box-card" style="width: 400px">
      <template #header>
        <div class="card-header">
          <span>{{ admin.name }}</span>
        </div>
      </template>
      <div>
        <div style="display: flex; justify-content: center; margin: 5px">
          <el-upload
              :headers="headers"
              :data="admin"
              :on-success="onSuccess"
              :show-file-list="false"
              action="/admin/userface">
            <img title="点击修改用户头像"
                 :src="admin.userFace"
                 style="height: 85px; width: 85px; border-radius: 50px;"/>
          </el-upload>
        </div>
        <div>电话号码：
          <el-tag>{{ admin.telephone }}</el-tag>
        </div>
        <div>手机号码：
          <el-tag>{{ admin.phone }}</el-tag>
        </div>
        <div>用户姓名：
          <el-tag>{{ admin.name }}</el-tag>
        </div>
        <div>居住地址：
          <el-tag>{{ admin.address }}</el-tag>
        </div>
        <div>
          用户标签：
          <el-tag v-for="(r, index) in admin.roles" :key="index">{{ r.name }}</el-tag>
        </div>
        <div style="margin-top: 5px; display: flex; justify-content: space-around">
          <el-dialog
              title="编辑用户信息"
              :visible.sync="dialogVisible"
              width="50%">
            <div>
              <table>
                <tr>
                  <td>用户昵称：</td>
                  <td>
                    <el-input v-model="editAdmin.name"></el-input>
                  </td>
                </tr>
                <tr>
                  <td>电话号码：</td>
                  <td>
                    <el-input v-model="editAdmin.telephone"></el-input>
                  </td>
                </tr>
                <tr>
                  <td>手机号码：</td>
                  <td>
                    <el-input v-model="editAdmin.phone"></el-input>
                  </td>
                </tr>
                <tr>
                  <td>居住地址：</td>
                  <td>
                    <el-input v-model="editAdmin.address"></el-input>
                  </td>
                </tr>

              </table>
            </div>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="doEditAdmin">确 定</el-button>
              </span>
            </template>
          </el-dialog>
          <el-button @click="dialogVisible = true" type="danger">修改信息</el-button>
          <el-dialog
              title="更新密码"
              :visible.sync="passwordDialogVisible"
              width="50%">
            <div>
              <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="请输入旧密码" prop="oldPass">
                  <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="请输入新密码" prop="pass">
                  <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                  <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                  <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-dialog>
          <el-button @click="updatePasswordDialog" type="warning">修改密码</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "AdminInfo",
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      headers: {
        Authorization: window.sessionStorage.getItem('tokenStr')
      },
      admin: null,
      editAdmin: null,
      dialogVisible: false,
      passwordDialogVisible: false,
      ruleForm: {
        oldPass: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        oldPass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
      }
    }
  },
  mounted() {
    this.initAdminInfo();
  },
  methods: {
    onSuccess() {
      this.initAdminInfo();
    },
    updatePasswordDialog() {
      this.passwordDialogVisible = !this.passwordDialogVisible;
    },
    doEditAdmin() {
      this.putRequest('/admin/info', this.editAdmin).then(resp => {
        if (resp) {
          this.initAdminInfo();
        }
      });
      this.dialogVisible = !this.dialogVisible;
    },
    initAdminInfo() {
      this.postRequest('/admin/info').then(resp => {
        if (resp) {
          this.admin = resp.obj;
          this.editAdmin = Object.assign({}, this.admin);
          window.sessionStorage.setItem('user', JSON.stringify(resp.obj));
          this.$store.commit('INIT_ADMIN', resp.obj);
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let updatePassDto = {
            adminId: this.admin.id,
            oldPass: this.ruleForm.oldPass,
            password: this.ruleForm.pass,
          }
          this.putRequest('/admin/pass', updatePassDto).then(resp => {
            if (resp) {
              //退出登录
              this.postRequest('/logout');
              window.sessionStorage.removeItem('user');
              window.sessionStorage.removeItem('tokenStr');
              this.$store.commit('initRoutes', []);
              this.$router.replace('/');
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>